<#include "/ftl/lib/operations.ftl"/>
<div class="easyui-layout" fit="true">  
	<div region="north" style="padding:5px;border:1px solid #ddd; background-color: #f4f4f4"  id="global_${action_id}" >
		<!-- <a href="javascript:;;" onclick="channel_add_${action_id}('${id?default('')}');" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">${action.getText('bg.datagrid.add')}</a> -->
	</div>  
	<div region="center" border="false">
		<div class="easyui-panel" data-options="href:'${request.contextPath}${namespace?default('')}/mp/product/type-list.htm?action_id=${action_id}',fit:true">
		</div>
	</div>  
</div>

<script type="text/javascript">

function add_${action_id}(){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/product/type-form.htm?action_id=${action_id}',
		width : 500,
		height : 400,
		modal : true,
		resizable:true,
		title : '${action.getText('bg.datagrid.add')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.add')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#form_${action_id}').form('submit', {
					url : '${request.contextPath}${namespace?default('')}/mp/product/type-add.json',
					onSubmit: function(){
						return $(this).form('validate');
    				},
					success : function(json) {
						eval("json="+json);
						$.messager.alert(json.title,json.msg,json.level);
						if(json.level!='error'){
							d.dialog('destroy');
							$("#datagrid_${action_id}").datagrid('reload');
						}
					}
				});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});	
}

function edit_${action_id}(data){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/product/type-edit.htm?ajax=false&action_id=${action_id}&id='+data.id,
		width : 500,
		height : 500,
		modal : true,
		resizable:true,
		title : '${action.getText('bg.datagrid.edit')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.edit')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#form_${action_id}').form('submit', {
					url : '${request.contextPath}${namespace?default('')}/mp/product/type-update.json',
					onSubmit: function(){
						return $(this).form('validate');
    				},
					success : function(json) {
						eval("json="+json);
						$.messager.alert(json.title,json.msg,json.level);
						if(json.level!='error'){
							d.dialog('destroy');
							$("#datagrid_${action_id}").datagrid('reload');
						}
					}
				});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}

function delete_${action_id}(data) {
	$.messager.confirm('${action.getProperty('net.esj.background.message','bg.message.info')}', '${action.getText('bg.datagrid.delete.ask')}', function(b) {
		if (b) {
			$.ajax({
				url : '${request.contextPath}${namespace?default('')}/mp/product/type-delete.json',
				data : {
					id : data.id
				},
				dataType : 'JSON',
				success : function(json) {
					$.messager.alert(json.title,json.msg,json.level);
					if(json.level!='error'){
						$("#datagrid_${action_id}").datagrid('reload');
					}
				}
			});
		}
	});
}

function attribute_${action_id}(data){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/product/type-attribute.htm?id='+data.id,
		width : 800,
		height : 500,
		modal : true,
		resizable:true,
		title : '${action.getText('bg.datagrid.edit')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.edit')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				var json = {};
				json['id'] = data.id;
				var rows = $("#attribute").datagrid("getRows");
				var falg = true;
				$.each(rows,function(i,row){
					if ($('#attribute').datagrid('validateRow', i)){
						var name =$('#attribute').datagrid('getEditor',{index:i,field:'name'});
						var type =$('#attribute').datagrid('getEditor',{index:i,field:'type'});
						var selectValue =$('#attribute').datagrid('getEditor',{index:i,field:'selectValue'});
						var unit =$('#attribute').datagrid('getEditor',{index:i,field:'unit'});
						var required =$('#attribute').datagrid('getEditor',{index:i,field:'required'});
						if(name && type && selectValue && unit && required){
							json['attributes['+i+'].id'] = row.id;
							json['attributes['+i+'].typeId'] = data.id;
							json['attributes['+i+'].name'] = name.target.val();
							json['attributes['+i+'].type'] = $(type.target).combobox('getValue');
							json['attributes['+i+'].state'] = row.state;
							json['attributes['+i+'].selectValue'] = selectValue.target.val();
							json['attributes['+i+'].unit'] = $(unit.target).combobox('getValue');
							json['attributes['+i+'].required'] = $(required.target).combobox('getValue');
						}else{
							json['attributes['+i+'].id'] = row.id;
							json['attributes['+i+'].typeId'] = data.id;
							json['attributes['+i+'].name'] = row.name;
							json['attributes['+i+'].type'] = row.type;
							json['attributes['+i+'].state'] = row.state;
							json['attributes['+i+'].selectValue'] = row.selectValue;
							json['attributes['+i+'].unit'] = row.unit;
							json['attributes['+i+'].required'] = row.required;
						}
					} else {
						falg = false;
						return;
					}
				});	
				if(falg){
					$.ajax({
						url : '${request.contextPath}${namespace?default('')}/mp/product/type-addAttributeList.json',
						data : json,
						dataType : 'JSON',
						success : function(json) {
							$.messager.alert(json.title,json.msg,json.level);
							if(json.level!='error'){
								//$("#attribute}").datagrid('reload');
								d.dialog('destroy');
							}
						}
					});
				}
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});	
}

function brand_${action_id}(data){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/product/type-brand.htm?id='+data.id,
		width : 500,
		height : 400,
		modal : true,
		resizable:true,
		title : '${action.getText('bg.datagrid.edit')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.save')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				var json = {};
				json['id'] = data.id;
				var count = $("#chhoose_brands").find("option").length;
				for(var i=0;i<count;i++){
					var value = $("#chhoose_brands").get(0).options[i].value;
					json['brandLinks['+i+'].id.brandId'] = value;
					json['brandLinks['+i+'].id.typeId'] = data.id;
				}
				$.ajax({
						url : '${request.contextPath}${namespace?default('')}/mp/product/type-addProductTypeBrandLink.json',
						data : json,
						dataType : 'JSON',
						success : function(json) {
							$.messager.alert(json.title,json.msg,json.level);
							if(json.level!='error'){
								//$("#attribute}").datagrid('reload');
								d.dialog('destroy');
							}
						}
					});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});	
}

function specifications_${action_id}(data){
	$('<div>').dialog({
		href : '${request.contextPath}${namespace?default('')}/mp/product/type-specifications.htm?id='+data.id,
		width : 500,
		height : 400,
		modal : true,
		resizable:true,
		title : '${action.getText('bg.datagrid.edit')}',
		buttons : [ {
			text : '${action.getText('bg.datagrid.save')}',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				var json = {};
				json['id'] = data.id;
				var count = $("#chhoose_specifications").find("option").length;
				for(var i=0;i<count;i++){
					var value = $("#chhoose_specifications").get(0).options[i].value;
					json['specificationsLinks['+i+'].id.specificationsId'] = value;
					json['specificationsLinks['+i+'].id.typeId'] = data.id;
				}
				$.ajax({
						url : '${request.contextPath}${namespace?default('')}/mp/product/type-addProductTypeSpecificationsLink.json',
						data : json,
						dataType : 'JSON',
						success : function(json) {
							$.messager.alert(json.title,json.msg,json.level);
							if(json.level!='error'){
								//$("#attribute}").datagrid('reload');
								d.dialog('destroy');
							}
						}
					});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}
</script>
<@operations contextUri='mp/product/type' />